package com.wushi.lenovo.asia5b.app_utils.helper;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;

import com.mvp.base.BaseApplication;
import com.mvp.utils.ResString;
import com.mvp.utils.ToastUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.CommonConstant;
import com.wushi.lenovo.asia5b.app_config.UserRepository;
import com.wushi.lenovo.asia5b.app_event.MainTabEvent;
import com.wushi.lenovo.asia5b.app_model.good.orderitem.OrderItemType;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_ui.h5.CategoryActivity;
import com.wushi.lenovo.asia5b.app_ui.h5.GoodsDetailsActivity;
import com.wushi.lenovo.asia5b.app_ui.inbox.activity.InboxActivity;
import com.wushi.lenovo.asia5b.app_ui.orderdetail.OrderDetailActivity;
import com.wushi.lenovo.asia5b.app_ui.other.activity.MainActivity;
import com.wushi.lenovo.asia5b.app_ui.profile.activity.ResetPassActivity;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import timber.log.Timber;

/**
 * Created by zhangyuncai on 2019/4/3.
 * 重置页面
 * asia5bnew.com://asia5bnew.com/login/resetpassword?memberid=1&validcode=123456
 * schme://host/登录模块/重置密码页面?会员id&验证code
 * schme://authority/path?query
 */
public class UriHelper {

    public static void parseUri(String url, Context context) {
        try {
            if (TextUtils.isEmpty(url)) {
                return;
            }
            parseUri(Uri.parse(url), context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 路由中心解析,只解析path和参数 譬如说 asia5b://www.asia5b.com/productDetail?id=1234
     * 那么只解析productDetail?id=1234这一段,前面都不用管
     * /productDetail?id=1234   商品详情
     * /goodDetail?id=1234   商品详情
     * /category?id=1234  商品分类
     * /openpage?id=1234  品牌分类
     * /exclusivebrands?id=5  活动分类
     * /login/resetpassword 重置密码
     * /h5/1688 重置密码
     * /homepage  主页
     * /inbox?id=0 消息主页 0表示inbox  1表示order
     * /orderDetail?id=1234 订单详情
     * /orderList?id=0 首页->订单列表 0/1/2/3/4 待审核/代付款/发货中/历史订单/问题
     *
     * @param uri
     * @param context
     */
    public static void parseUri(Uri uri, Context context) {
        try {
            if (uri == null) {
                Timber.d("--->>>>>>>>---uri==null");
                return;
            }
            Timber.d("--->>>>>>>>---uri:" + uri.toString());
            String path = uri.getPath();
            if (TextUtils.isEmpty(path)) {
                return;
            }
            Timber.d("--->>>>>>>>---path:" + path);
            switch (path) {
                case "/homepage": {//打开主页
                    context.startActivity(new Intent(context, MainActivity.class));
                }
                break;
                case "/orderList": {//首页订单列表
                    if (UserRepository.isLogin(context)) {
                        context.startActivity(new Intent(context, MainActivity.class));
                        int id = Integer.parseInt(uri.getQueryParameter("id"));
                        EventBus.getDefault().post(new MainTabEvent(3, id));
                    }
                }
                break;
                case "/orderDetail": {//订单详情
                    if (UserRepository.isLogin(context)) {
                        int id = Integer.parseInt(uri.getQueryParameter("id"));
                        OrderDetailActivity.startOrderDetailActivity(context, id, OrderItemType.Verification);
                    }
                }
                break;
                case "/inbox": {//消息页面
                    if (UserRepository.isLogin(context)) {
                        int id = Integer.parseInt(uri.getQueryParameter("id"));
                        Intent intent = new Intent(context, InboxActivity.class);
                        intent.putExtra("id", id);
                        context.startActivity(intent);
                    }
                }
                break;
                case "/productDetail":
                case "/productInfo": {//商品详情
                    if (UserRepository.isLogin(context)) {
                        int id = Integer.parseInt(uri.getQueryParameter("id"));
                        String url = H5Hepler.getH5_Url(String.format("goods?id=%d", id));
                        GoodsDetailsActivity.startGoodsDetailsActivity(context, url);
                    }
                }
                break;
                case "/category": {//商品分类
                    int id = Integer.parseInt(uri.getQueryParameter("id"));
                    String url = H5Hepler.getH5_Url(String.format("cat_2?cat_id_1=%d", id));
                    CategoryActivity.startCategoryActivity(context, url, null);
                }
                break;
                case "/openPage": {//品牌分类,/openpage?openpage_id=7
                    int id = Integer.parseInt(uri.getQueryParameter("id"));
                    String url = H5Hepler.getH5_Url(String.format("/openpage?openpage_id=%d", id));
                    CategoryActivity.startCategoryActivity(context, url, null);
                }
                break;
                case "/exclusivebrands": {//活动分类,/exclusivebrands?exclusive_id=5
                    int id = Integer.parseInt(uri.getQueryParameter("id"));
                    String url = H5Hepler.getH5_Url(String.format("/exclusivebrands?exclusive_id=%d", id));
                    CategoryActivity.startCategoryActivity(context, url, null);
                }
                break;
                case "/login/resetpassword": {//重置密码
                    String memberidStr = uri.getQueryParameter("memberid");
                    String validcode = uri.getQueryParameter("validcode");
                    int memberid = Integer.parseInt(memberidStr);
                    if (memberid > 0 && !TextUtils.isEmpty(validcode)) {

                        if ((UserRepository.isLogin() && UserInfo.getMemberId() == memberid) //登录且修改当前用户密码
                                || (!UserRepository.isLogin())) {//未登录修改输入的用户密码
                            Intent intent = new Intent(context, ResetPassActivity.class);
                            intent.putExtra(CommonConstant.RESET_LOGIN_FORGOT_CODE, validcode);
                            context.startActivity(intent);
                        } else if (UserRepository.isLogin() && ((UserInfo.getMemberId() != memberid))) {//禁止修改不属于自己的用户密码
                            ToastUtil.showToast(ResString.get(R.string.doesnotupdatepass));
                        }
                    }
                }
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Uri getUri(File file) {
        Uri uri;
        if (Build.VERSION.SDK_INT >= 24) {
            uri = FileProvider.getUriForFile(BaseApplication.sContext, "com.wushi.lenovo.asia5b.fileProvider", file);
        } else {
            uri = Uri.fromFile(file);
        }
        return uri;
    }
}
